<?php
class CouncilMembersController extends AppController {

	var $name = 'CouncilMembers';

	function index() {
		$this->CouncilMember->recursive = 0;
		$this->set('councilMembers', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid council member', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('councilMember', $this->CouncilMember->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->CouncilMember->create();
                        $this->data['CouncilMember']['end_date'] = null;
			if ($this->CouncilMember->save($this->data)) {
				$this->Session->setFlash(__('The council member has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The council member could not be saved. Please, try again.', true));
			}
		}
		$buildings = $this->CouncilMember->Building->find('list');
		$people = $this->CouncilMember->Person->find('list');
		$this->set(compact('buildings', 'people'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid council member', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->CouncilMember->save($this->data)) {
				$this->Session->setFlash(__('The council member has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The council member could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->CouncilMember->read(null, $id);
		}
		$buildings = $this->CouncilMember->Building->find('list');
		$people = $this->CouncilMember->Person->find('list');
		$this->set(compact('buildings', 'people'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for council member', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->data['CouncilMember']['end_date'] = date('Y-m-d H:i:s');
                if ($this->CouncilMember->save($this->data)) {
                    $this->Session->setFlash(__('Council member deleted', true));
                } else {
                    $this->Session->setFlash(__('Council member was not deleted', true));
                }
		$this->redirect(array('action' => 'index'));
	}
}
?>